<?php

class DicasdebelezaController extends Site_Controller_Action
{
    public function init()
    {
        $this->body_class = 'right-sidebar';
    }

    public function indexAction()
    {
        Zend_Loader::loadClass('datetools','library');
        
        $result = $this->db->fetchAll('SELECT SQL_CACHE * FROM ST_CATEGORIA_BLOG WHERE CTB_STA = 1 ORDER BY CTB_NOM ASC');
        $this->view->cats = $result;
        
        $id = $this->_getParam('id', 0);
        if($id > 0){
            $result = $this->db->fetchRow('SELECT SQL_CACHE * FROM ST_BLOG WHERE BLO_ID = '.$id.' AND BLO_STA = 1');
            $this->view->blog = $result; 
        }
        else{
            
            // categoria
            $cat = $this->_getParam('cat', 0);
            if($cat > 0) $where = ' AND c.CTB_ID = '.$cat;

            // Mes
            $ano = $this->_getParam('ano', 0);
            $mes = $this->_getParam('mes', 0);
            if($ano > 0 && $mes > 0) $where = ' AND YEAR(BLO_DAT) like "'.$ano.'" AND MONTH(BLO_DAT) like "'.$mes.'"';
            
            $query = 'SELECT SQL_CACHE b.*,c.CTB_NOM FROM ST_BLOG b, ST_CATEGORIA_BLOG c WHERE b.CTB_ID = c.CTB_ID '.$where.' ORDER BY b.BLO_DAT DESC';
            $result = $this->db->fetchAll($query);
            $this->view->blogs = $this->paginacao($result);
        }
        
        $query = 'SELECT SQL_CACHE MONTH(BLO_DAT) as mes, YEAR(BLO_DAT) as ano FROM ST_BLOG
                    WHERE BLO_STA = 1
                    GROUP BY YEAR(BLO_DAT),MONTH(BLO_DAT) 
                    ORDER BY YEAR(BLO_DAT) DESC,MONTH(BLO_DAT) DESC';
        $obj = $this->db->fetchAll($query);
        $this->view->meses = $obj;
        
    }
    
}

